#include "stopwatchwidget.h"

StopWatchWidget::StopWatchWidget(QWidget *parent): QWidget{parent}
{

    //定时器动态增加时速
    myTimer = new QTimer(this);
    connect(myTimer,SIGNAL(timeout()),this,SLOT(slot_speed_changed()));
}



void StopWatchWidget::paintEvent(QPaintEvent*)
{
    QPainter painter(this);
    int width=this->width();
    int height=this->height();//移动仪表盘的高度
    int radius=((width>height)?height:width)/1.6;//仪表盘的中心位置
    //移动画笔到中下方
    painter.translate(width/2,height/2);
    //启用反锯齿
    painter.setRenderHint(QPainter::Antialiasing, true);
    painter.setPen(Qt::NoPen);
    //设置画刷颜色
    painter.setBrush(QColor(138,43,226));
    DrawSmallScale(painter,radius-60);//刻度线
    DrawDigital(painter,radius-80);//刻度数字
    /*所有形状绘画*/
    DrawPointer(painter,radius-130);//指针

    DrawUnit(painter,radius);//单位
    DrawNum(painter,radius-280);//时速
}
//绘制外圈点
void StopWatchWidget::DrawPoint(QPainter& painter,int radius)
{
    //组装点的路径图
    QPainterPath pointPath;
    pointPath.moveTo(-2,-2);
    pointPath.lineTo(2,-2);
    pointPath.lineTo(2,2);
    pointPath.lineTo(0,4);
    pointPath.lineTo(-2,2);
    //绘制13个小点
    for(int i=0;i<13;++i){
        QPointF point(0,0);
        painter.save();
        painter.setBrush(QColor(250,252,78));
        //计算并移动绘图对象中心点
        point.setX(radius*qCos(((210-i*20)*M_PI)/180));
        point.setY(radius*qSin(((210-i*20)*M_PI)/180));
        //计算并移动绘图对象的中心点
        painter.translate(point.x(),-point.y());
        //计算并选择绘图对象坐标
        painter.rotate(-120+i*20);
        //绘制路径
        painter.drawPath(pointPath);
        painter.restore();
    }
}

void StopWatchWidget::DrawDigital(QPainter& painter,int radius)
{
    //设置画笔，画笔默认NOPEN
    painter.setPen(QColor(255,255,255));
    QFont font;
    font.setFamily("Arial");
    font.setPointSize(15);
    font.setBold(true);
    painter.setFont(font);
    for(int i=0;i<13;++i){
        QPointF point(0,0);
        painter.save();
        point.setX(radius*qCos(((210-i*20)*M_PI)/180));
        point.setY(radius*qSin(((210-i*20)*M_PI)/180));
        painter.translate(point.x(),-point.y());
        painter.rotate(-120+i*20);
        painter.drawText(-25, 0, 50, 20,Qt::AlignCenter,QString::number(i*10));
        painter.restore();
    }
    //还原画笔
    painter.setPen(Qt::NoPen);
}



//绘制刻度
void StopWatchWidget::DrawSmallScale(QPainter& painter,int radius)
{
    //组装点的路径图
    QPainterPath pointPath_small;
    pointPath_small.moveTo(-0.5,-0.5);
    pointPath_small.lineTo(0.5,-0.5);
    pointPath_small.lineTo(0.5,10);
    pointPath_small.lineTo(-0.5,10);


    QPainterPath pointPath_big;
    pointPath_big.moveTo(-0.5,-0.5);
    pointPath_big.lineTo(0.5,-0.5);
    pointPath_big.lineTo(0.5,15);
    pointPath_big.lineTo(-0.5,15);

    //绘制121个小点
    for(int i=0;i<121;i+=1){
        QPointF point(0,0);
        painter.save();
        point.setX(radius*qCos(((210-i*2)*M_PI)/180));
        point.setY(radius*qSin(((210-i*2)*M_PI)/180));
        painter.translate(point.x(),-point.y());
        painter.rotate(-120+i*2);

        painter.setBrush(QColor(255,255,255));

        if(i%5 == 0)
        {
            painter.drawPath(pointPath_big);//绘画大刻度
        }else
        {
            painter.drawPath(pointPath_small);//绘画小刻度
        }
        painter.restore();
    }
}

//绘制文字
void StopWatchWidget::DrawUnit(QPainter& painter,int radius)
{
    painter.save();
    painter.setPen(QColor(255,255,255));
    QFont font;
    font.setPointSize(14);
//    font.setBold(true);
    painter.setFont(font);
    painter.drawText(-50, -radius+250, 100, 20,Qt::AlignCenter,QString("km/h"));
    painter.restore();

    painter.save();
    painter.setPen(QColor(255,255,255));
    font.setPointSize(20);
    font.setBold(true);
    painter.setFont(font);
    painter.drawText(-60, -radius + 330, 120, 40,Qt::AlignCenter,QString("CM"));
    painter.restore();

    painter.save();
    painter.setPen(QColor(0,153,61));
    font.setPointSize(10);
    painter.setFont(font);
   painter.drawText(-250, -radius + 330, 250, 40,Qt::AlignCenter,QString("25\nKm/hr"));
    painter.restore();

}

//绘制实时时速数字
void StopWatchWidget::DrawNum(QPainter& painter,int radius)
{
    painter.save();
    painter.setPen(QColor(255,255,255));
    QFont font;
    font.setFamily("Arial");
    font.setPointSize(45);
    painter.setFont(font);
    painter.drawText(-75, -radius - 20, 150, 100,Qt::AlignCenter,QString::number(degRotate));
    painter.restore();
}

//绘制指针
void StopWatchWidget::DrawPointer(QPainter& painter,int radius)
{
    //组装点的路径图
    QPainterPath pointPath;
    pointPath.moveTo(10,0);
    pointPath.lineTo(1,-radius);
    pointPath.lineTo(-1,-radius);
    pointPath.lineTo(-10,0);
    pointPath.arcTo(-10,0,20,20,180,180);
    QPainterPath inRing;
    inRing.addEllipse(-5,-5,10,10);
    painter.save();

    //计算并选择绘图对象坐标
    painter.rotate(degRotate - 120);
    painter.setBrush(QColor(255,255,255));
    painter.drawPath(pointPath.subtracted(inRing));
    painter.restore();
}


//动态增加时速画面效果
void StopWatchWidget::slot_speed_changed()
{
    if(direction == 1)//加速
    {
        degRotate++;
        if(degRotate > 240)
            degRotate = 240;
    }
    else if(direction == 0)//减速
    {
        degRotate--;
        if(degRotate < 0)
        {
            degRotate = 0;
            myTimer->stop();
        }
    }
    update();//刷新画面。很重要！
}

//按键按下事件
void StopWatchWidget::keyPressEvent(QKeyEvent *event)
{
    if(event->key() == Qt::Key_Space)
    {
        if(direction == 0)
        {
            myTimer->start(1);
            direction = 1;
        }

    }
}
//按键释放事件
void StopWatchWidget::keyReleaseEvent(QKeyEvent *event)
{
    if(event->key() == Qt::Key_Space)
    {
        direction = 0;
    }
}

StopWatchWidget::~StopWatchWidget()
{

}
